package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataGetModel;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataModel;
import com.hzhr.iot.device.sdk.service.Device_AlarmDataService;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("/alarmdata")
@Api(description = "报警信息")
@RestController
public class AlarmDataController extends BaseController {
    @Autowired
    Device_AlarmDataService device_alarmDataService;


    @ApiOperation(value = "报警列表", notes = "报警列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "单页面条数", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "iotdeviceId", value = "物联网设备ID", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "collectdeviceId", value = "采集设备ID", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "begintime", value = "开始时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "endtime", value = "结束时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "isvalid", value = "状态,1启用 0停用 -1全部", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "alarmtype", value = "异常状态类型", required =false,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "安装地址", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping(value = "/getalarmlist", method = RequestMethod.POST)
    BaseResponse getalarmlist(@RequestParam(value ="pageNum", required = false) Integer pageNum,
                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
                              @RequestParam(value = "uat", required = true) String uat,
                              @RequestParam(value = "id", required = false) String id,
                              @RequestParam(value = "iotdeviceId", required = false) String iotdeviceId,
                              @RequestParam(value = "collectdeviceId", required = false) String collectdeviceId,
                              @RequestParam(value = "begintime", required = false) String begintime,
                              @RequestParam(value = "endtime", required = false) String endtime,
                              @RequestParam(value = "isvalid", required = false) Integer isvalid,
                              @RequestParam(value = "alarmtype", required = false) String alarmtype,
                              @RequestParam(value = "address", required = false) String address) throws Exception {
        Map<String, Object> result = new HashMap<>();

        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, Object> query = new HashMap<>();
        if (!StringUtils.isEmpty(collectdeviceId)) {
            query.put("equal_collectDeviceId", collectdeviceId);
        }
        if (!StringUtils.isEmpty(iotdeviceId)) {
            query.put("equal_iotDeviceId", iotdeviceId);
        }
        if (!StringUtils.isEmpty(begintime)) {
            query.put("greaterthanorequal_currentTm", HzhrDateUtil.strToDate(begintime).getTime());
        }
        if (!StringUtils.isEmpty(endtime)) {
            query.put("lessthan_currentTm", HzhrDateUtil.strToDate(endtime).getTime());
        }
        query.put("show", "list");
        query.put("show_info", true);
        query.put("show_total", true);
        query.put("orderBy_currentTm", "desc");
        query.put("page", pageNum);
        query.put("count", pageSize);
        AlarmDataGetModel alarmDataGetModel = device_alarmDataService.get(header, query);
        List<AlarmDataModel> alarmDataModels = alarmDataGetModel.getModelList();
        int total = alarmDataGetModel.getTotal();

        //组装数据
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (AlarmDataModel alarmDataModel :
        alarmDataModels) {
            Map<String, Object> data = new HashMap<>();

            String currentTm = HzhrTypeUtil.object2string(alarmDataModel.getCurrentTm());
            if (!StringUtils.isEmpty(currentTm)) {
                data.put("currenttime", HzhrDateUtil.dateToStr(new Date(Long.parseLong(currentTm))));
            }

            Map<String, Object> deviceCollectdevice_collectdeviceId = new HashMap<>();
            deviceCollectdevice_collectdeviceId.put("deviceid", null);
            data.put("deviceCollectdevice_collectdeviceId", deviceCollectdevice_collectdeviceId);

            Map<String, Object> options_alarmtype = new HashMap<>();
            options_alarmtype.put("remarks", alarmDataModel.getAlarmTypeDes());
            data.put("options_alarmtype", options_alarmtype);

            dataList.add(data);
        }

        result.put("dataList", dataList);
        result.put("total", total);

        return new BaseResponse("0", "成功",result);
    }
}
